package transacoes;

import utils.*;
import data.*;
import java.util.*;
import utils.Transacao;

public class User {

  public boolean incluir (UserDO user) throws Exception{
     // validacao das regras de negocio
     if ( ((isEmpty_int(user.getIdFuncionario())) || ((user.getLogin() == null)) || (user.getPassword() == null)) || 
          ((isEmpty_int(user.getIdPaciente()))) ) {
       return false;
     }
     // efetuando a transacao
     Transacao tr = new Transacao();
     try {

       tr.begin();
         UserData cdata = new UserData();
         cdata.incluir(user, tr);
       tr.commit();
       return true;
       
     } catch(Exception e) {
         tr.rollback();
         System.out.println("erro ao incluir " + user.getIdUser());
         e.printStackTrace();
     }
     return false;
  } // incluir
 
  public boolean excluir (UserDO user) throws Exception{

     // validacao das regras de negocio
     if ( (isEmpty_int(user.getIdUser()))) {
       return false;
     }

     // efetuando a transacao
     Transacao tr = new Transacao();
     try {

       tr.begin();
         UserData udata = new UserData();
         udata.excluir(user, tr);
       tr.commit();
       return true;
       
     } catch(Exception e) {
         tr.rollback();
         System.out.println("erro ao excluir " + user.getIdUser());
         e.printStackTrace();
     }
     return false;
  } // excluir

  public boolean atualizar(UserDO user) throws Exception {
     Transacao tr = new Transacao();
         try{
           // inserir validacoes de regras de negocio
           tr.begin();
             UserData udata = new UserData();
             udata.atualizar(user, tr);
           tr.commit();
           return true;
         } catch (Exception e) {
           tr.rollback();
           System.out.println("erro ao atualizar" + user.getIdUser());
           e.printStackTrace();
         }
         return false;
  } // atualizar
    
  public UserDO buscar(int Login) throws Exception{
     Transacao tr = new Transacao();
         try{
           tr.beginReadOnly();
             UserData Udata = new UserData();
             UserDO p = Udata.buscar(Login, tr);
           tr.commit();
           return p;
         } catch (Exception e) {
           tr.rollback();
           System.out.println("Login Nao Encontrado");
           e.printStackTrace();
         }
         return null;
  } // buscar
  
  public UserDO buscarPorNome(String Login) throws Exception{
     Transacao tr = new Transacao();
         try{
           tr.beginReadOnly();
             UserData Udata = new UserData();
             UserDO p = Udata.pesquisarPorNome(Login, tr);
           tr.commit();
           return p;
         } catch (Exception e) {
           tr.rollback();
           System.out.println("Login Nao Encontrado");
           e.printStackTrace();
         }
         return null;
  } // buscarPorNome
  
  private boolean isEmpty(String s) {
     if (null == s)
       return true;
     if (s.length() == 0)
       return true;
     return false;
  }
  
  private boolean isEmpty_int(int i) {
     if (0 == i){return true;}
     else{return false;}
  }
}